package com.frogs.ucenter.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.UserFeedback;
import com.frogs.ucenter.service.UserFeedbackService;

@Controller
@RequestMapping("/admin/user/feedback")
public class UserFeedbackController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userFeedback";

    private static final String HOME_VIEW = "admin/userFeedback/home";

    private static final String EDIT_VIEW = "admin/userFeedback/edit";

    @Resource
    private UserFeedbackService userFeedbackService;
    
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;

    @RequestMapping()
    public String listUserFeedback(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserFeedback(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",userFeedbackService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(UPDATE)
    public String updateUserFeedback(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
    	setRequestModelMap( request, model, false );
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
//            	permissionHelper.init(request, response);
            	User user = permissionHelper.getUser();
            	UserFeedback userFeedback = userFeedbackService.getById( id );
            	userFeedback.setStatus(Integer.valueOf((String)model.get("status")));
            	userFeedback.setResult((String)model.get("result"));
                userFeedback.setAdminId(user.getUserId());
                userFeedback.setAdminName(user.getFullName());
                userFeedback.setUpdateTime(new Date());
                userFeedbackService.update( userFeedback ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("修改成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("修改失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    private PageList<UserFeedback> getPageList(ModelMap model) {
    	model.put("orderByClause", " create_time desc ");
        PageList<UserFeedback> userFeedbackList = userFeedbackService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userFeedbackList",userFeedbackList);
        model.put("pageTurn",userFeedbackList.getPageTurn());
        return userFeedbackList;
    }
}